<template>
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="dePForm">
    <el-form-item label="职务名称" prop="name">
      <el-input v-model="ruleForm.name"></el-input>
    </el-form-item>
    <el-form-item label="职务权限" prop="power">
      <el-select
          v-model="ruleForm.value2"
          style="width: 300px"
          multiple
          placeholder="请选择"
          @change = changeGetLable>
        <el-option
            v-for="item in ruleForm.options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="职位描述" prop="desc">
      <el-input type="textarea" v-model="ruleForm.desc" :rows="5"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
      <el-button @click="back">取消</el-button>
    </el-form-item>
  </el-form>

</template>

<script>
import {updataJob, addJobList, getJobListinfo} from "../../../api/api";

export default {
  data() {
    return {
      ruleForm: {
        name: '',
        value2: '',
        desc: '',
        options: [{
          value: '选项1',
          label: 'userhandle'
        }, {
          value: '选项2',
          label: 'departhandle'
        }, {
          value: '选项3',
          label: 'jobhandle'
        }, {
          value: '选项4',
          label: 'departcustomer'
        }, {
          value: '选项5',
          label: 'allcustomer'
        },{
          value: '选项6',
          label: 'resetpassword'
        }]
      },
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '请填写描述', trigger: 'blur' }
        ],
      }
    };
  },
  created() {
    // 如果id存在，在获取对应的详细信息，不存在，则方法不执行
    this.id = this.$route.query.id
    this.id && this.getJobInfo()
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 判断是否要执行更新操作
          let str = this.changeGetLable()
          if(this.id) {
            updataJob({
              jobId: this.id,
              name: this.ruleForm.name,
              desc: this.ruleForm.desc,
              power: str
            }).then(data => {
              if (data.code ===0) {
                this.$alert('修改成功', '提示', {
                  confirmButtonText: '确定',
                  callback: action => {
                    this.$router.push('/org/job')
                  }
                })
              }else {
                this.$alert('修改失败', '提示', {
                  confirmButtonText: '确定',
                  callback: action => {}
                })
              }
            })
          }else {
            addJobList({
              name: this.ruleForm.name,
              desc: this.ruleForm.desc,
              power: str
            }).then(data => {
              if (data.code ===0) {
                // 新增成功
                this.$alert('新增成功', '提示', {
                  confirmButtonText: '确定',
                  callback: action => {
                    // 点击确定我们跳转到列表页
                    this.$router.push('/org/job')
                  }
                })
              } else {
                this.$alert('新增失败', '提示', {
                  confirmButtonText: '确定',
                  callback: action => {}
                })
              }
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 获取多选项对应的lable值
    changeGetLable() {
      let values = this.ruleForm.value2,
          arrvalue = [],
          options = this.ruleForm.options
      values.forEach(item => {
        for (let i = 0; i<options.length; i++) {
          if (item === options[i].value) {
            arrvalue.push(options[i].label)
          }
        }
      })
      return arrvalue.join('|')
    },
    // 获取用户详细信息
    getJobInfo(){
      let id = this.$route.query.id
      getJobListinfo(id).then(data => {
        let value2 = this.putLable(data.data.power)
        this.ruleForm.name = data.data.name
        this.ruleForm.desc = data.data.desc
        this.ruleForm.value2 = value2
      })
  },
    // 根据返回的数据将多选的值选上
    putLable(str) {
      let strArr = str.split('|')
      let valuearr = [],
          options = this.ruleForm.options
      strArr.forEach(item => {
        for (let i = 0;i < options.length; i++) {
          if (item === options[i].label) {
            valuearr.push(options[i].value)
          }
        }
      })
      return valuearr
    },
    // 取消职务修改
    back() {
      this.$router.back()
    }

  }
}
</script>

<style scoped>
.dePForm {
  text-align: left;
  width: 450px;
}
.el-textarea__inner {
  height: 200px;
}
</style>
